package utils

import (
	"context"
	"fmt"
	"time"

	"github.com/beego/beego/v2/server/web"
	"github.com/go-redis/redis/v8"
)

var (
	MYSQL_URL string //数据库连接

	Re error         //redis错误
	Rc *redis.Client //redis缓存
)

func init() {

	tmpRunMode, err := web.AppConfig.String("run_mode")
	if err != nil {
		panic(any("配置文件读取错误 " + err.Error()))
	}
	RunMode := tmpRunMode
	if tmpRunMode == "" {
		RunMode = "dev"
	}

	config, err := web.AppConfig.GetSection(RunMode)
	if err != nil {
		panic(any("配置文件读取错误 " + err.Error()))
	}
	redisClient, err := initRedis(config["beego_cache"])
	if err != nil {
		fmt.Println("redis链接异常：", err)
		panic(any(Re))
	}

	Rc = redisClient

	err = Rc.Set(context.Background(), "test", "res323", 0).Err()
	if err != nil {
		fmt.Println("redis set异常：", err)
		panic(any(Re))
	}
	now := time.Now()
	res, err := Rc.Get(context.Background(), "test").Result()
	if err == nil {
		fmt.Printf("redis 查询耗时:%d, res:%s \n", time.Since(now).Milliseconds(), res)
	}

	MYSQL_URL = config["mysql_url"]
}
